package com.sxkf.template.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxkf.core.api.vo.R;
import com.sxkf.template.constants.CommonConstants;
import com.sxkf.template.constants.ViewTypeConstants;
import com.sxkf.template.entity.ProductView;
import com.sxkf.template.entity.Store;
import com.sxkf.template.entity.StoreEvaluation;
import com.sxkf.template.entity.User;
import com.sxkf.template.service.ProductViewService;
import com.sxkf.template.service.StoreEvaluationService;
import com.sxkf.template.service.StoreService;
import com.sxkf.template.service.UserService;
import com.sxkf.template.vo.request.store.QueryStoreVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.util.CheckEmptyUtil;
import org.jeecg.common.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Set;

/**
 * (t_store)表控制层
 *
 * @author zgc
 */
@Api(tags = "商铺模块管理")
@RestController
@RequestMapping("/store")
public class StoreController {
    /**
     * 服务对象
     */
    @Resource
    private StoreService storeService;

    @Resource
    private ProductViewService productViewService;

    @Resource
    private UserService userService;

    @Resource
    private RedisUtils redisUtils;


    @ApiOperation("首页附近店铺")
    @GetMapping("/listNearby")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "longitude", value = "用户所在经度"),
            @ApiImplicitParam(name = "latitude", value = "用户所在纬度")
    })
    public R<IPage<Store>> listNearby(@RequestParam Double longitude, @RequestParam Double latitude,
                                      @RequestParam Integer pageNo, @RequestParam Integer pageSize) {
        return R.success(storeService.listNearby(longitude, latitude, pageNo, pageSize));
    }

    @ApiOperation(value = "用户查询店铺")
    @GetMapping(value = "/listByStore")
    public R<IPage<Store>> listByStore(QueryStoreVo queryStoreVo){
        IPage<Store> result = storeService.listByStore(queryStoreVo);
        return R.success(result);
    }

    @ApiOperation("店铺搜索历史记录")
    @GetMapping("/getHis")
    public R<List<String>> getHis(@RequestParam String userId){
        List<String> hisSearch = redisUtils.lGet(CommonConstants.HIS_SEARCH + ":" + userId, 0, -1);
        return R.success(hisSearch);
    }

    @ApiOperation("清空店铺搜索历史记录")
    @GetMapping("/delHis")
    public R<?> delHis(@RequestParam String userId){
        redisUtils.del(CommonConstants.HIS_SEARCH + ":" + userId);
        return R.success();
    }

    @ApiOperation("搜索发现")
    @GetMapping("/search")
    public R<Set<String>> search(){
        Set<String> range = redisUtils.rangeByScoreDesc(CommonConstants.SEARCH_FIND, 0, -1);
        return R.success(range);
    }

    
    @ApiOperation("店铺主页详情")
    @GetMapping("storeDetail")
    public R<Store> storeDetail(@RequestParam String storeId,
                                @RequestParam(required = false) String userId) {

        // 加了个足迹
        if (CheckEmptyUtil.isNotEmpty(userId)){
            long count = productViewService.count(new LambdaQueryWrapper<ProductView>()
                    .eq(ProductView::getUserId, userId)
                    .eq(ProductView::getViewId, storeId)
                    .eq(ProductView::getViewType, ViewTypeConstants.STORE));
            if (count == 0){
                User user = userService.getById(userId);
                ProductView productView = new ProductView();
                productView.setViewId(storeId);
                productView.setViewType(ViewTypeConstants.STORE);
                productView.setUserId(userId);
                productView.setUserNickName(user.getNickName());
                productView.setUserAvatar(user.getAvatar());
                productViewService.save(productView);
            }

        }

        return R.success(storeService.getById(storeId));
    }

    @ApiOperation("店铺信息修改")
    @PostMapping("/update")
    public R<Boolean> update(@RequestBody Store store) {
        return R.success(storeService.updateById(store));
    }


    @ApiOperation("分页查店铺")
    @GetMapping("/page")
    public R<IPage<Store>> page(@RequestParam Integer pageNo,
                                @RequestParam Integer pageSize) {
        Page<Store> page = storeService.page(new Page<>(pageNo, pageSize));
        return R.success(page);
    }

    @ApiOperation("删除店铺")
    @GetMapping("/del")
    public R<?> delete(@RequestParam String id) {
        storeService.removeById(id);
        return R.success();
    }

    @ApiOperation("店铺评分")
    @GetMapping("/evaluation")
    public R<String> evaluation(@RequestParam String storeId,
                                @RequestParam String userId,
                                @RequestParam Double evaluation) {
        boolean res = storeService.evaluation(storeId,userId,evaluation);

        if (res) {
            return R.success("评分成功");
        }
        return R.error("评分失败");

    }

    @ApiOperation("获取运费")
    @GetMapping("/getFreight")
    public R<Double> getFreight(@RequestParam String storeId) {
        Store store = storeService.getById(storeId);
        return R.success(store.getFreight().doubleValue());

    }



}
